Many of the maintenance features discussed so far in
this chapter are important, but they can be time-consuming. However,
that doesn’t necessarily have to be the case. Essentially any task that
can be performed from SQL Server Management Studio or through the use
of a T-SQL query can be automated through the creation of a job or a
schedule.
Wouldn’t it
be nice to have an agent working on your behalf to get the regular
database maintenance activities done on a scheduled basis? Of course,
the agent would have to provide feedback when things didn’t quite go as
expected. It would also be essential for summaries to be received from
time to time, perhaps by email, providing documentation of exactly what
had happened, when it was performed, and what activities failed.
Fortunately, SQL Server framework has just such functionality: the SQL
Server Agent.
The
SQL Server Agent runs as a Microsoft Windows service. When running, the
agent executes scheduled tasks. Each task is defined within SQL Server
as a job. The agent performs the steps defined in the job and stores
the job information and history within the msdb database. SQL Server Agent can run a job on a schedule, in response to a specific event, or on demand.
Most
jobs contain multiple steps, although it is possible for a job to be
made up of a singular process. You define a workflow to instruct the
agent on how to proceed between the individual tasks. The workflow
allows for decision making in a job with a variety of outcomes, based
on the results of completing or not completing steps within the job.